package com.kavinz.algo.string.other;

/**
 * Cracking the coding interview》- #Interview Questions 1.4 Write a method to
 * decide if two strings are anagrams or not
 * 
 * note:assume the alphabet is ASCII(256 characters)
 * @author Kevin Zhao
 */
public class Anagrams {

	/**
	 * compare the ASCII sum of two string
	 * 
	 * <br>Warning:this function will produce wrong result when two string have same ASCII sum but different characters
	 * @param A
	 * @param B
	 * @return
	 */
	@Deprecated
	public static boolean isAnagram(String A,String B)
	{
		//calculate the ASCII sum of all the characters in the stringA
		int charSumA = 0;
		for(char character:A.toCharArray())
		{
			charSumA += character;
		}
		//calculate the ASCII sum of all the characters in the stringB		
		int charSumB = 0;
		for(char character:B.toCharArray())
		{
			charSumB += character;
		}
		//compare two ASCII sum
		return charSumA == charSumB;
	}
	
	
	
	public static void main(String[] args) {
		System.out.println(Anagrams.isAnagram("ciname", "cnamie"));
		System.out.println(Anagrams.isAnagram("ccna", "ancb"));
		System.out.println(Anagrams.isAnagram("aborad", "boarad"));
		System.out.println(Anagrams.isAnagram("aabdi", "dsgi"));
		System.out.println(Anagrams.isAnagram("ac", "bb"));//Anagrams.isAnagram() will produce wrong result under this test case
	}
}
